package com.molichuxing.services.infrastructure.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.molichuxing.framework.utils.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.StockStoreBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StockStoreMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StockStoreCreateDto;
import com.molichuxing.services.infrastructure.dto.response.StockStoreDto;
import com.molichuxing.services.infrastructure.service.StockStoreService;

/**
 * 
 * @author liyg
 * 门店仓库服务实现类
 * 2019-08-21
 *
 */
@Service("stockStoreService")
public class StockStoreServiceImpl implements StockStoreService{
	
	@Autowired
	private StockStoreMapper stockStoreMapper;

	/**
	 * 获取门店仓库信息
	 * 
	 * @param stockId
	 * @return
	 */   
	@Override
	public StockStoreDto getStockStore(Integer stockId) {
		
		StockStoreBean stockStore= stockStoreMapper.getByStockId(stockId);
		StockStoreDto dto = Convert.toStockStoreDto(stockStore);
		
		return dto;
	}
	
	/**
	 * 获取门店仓库信息
	 * 
	 * @param stockIds
	 * @return
	 */  
	@Override
	public Map<Integer, StockStoreDto> getStockStores(List<Integer> stockIds) {
		
		if(null == stockIds || stockIds.size() <= 0) {
			return null;
		}
		
		List<StockStoreBean> list=stockStoreMapper.getByStockIds(stockIds);
		List<StockStoreDto> stockStoreDtoList = Convert.toStockStoreDto(list);
		if(null == stockStoreDtoList || stockStoreDtoList.size() <= 0) {
			return null;
		}
		
		int initialCapacity = stockStoreDtoList.size();
		Map<Integer, StockStoreDto> map=new HashMap<>(initialCapacity);
		for (StockStoreDto dto : stockStoreDtoList) {
			map.put(dto.getStockId(), dto);
		}
		
		return map;
	}
	
	
	@Override
	public Map<Integer, StockStoreDto> getStockStoresByStoreIds(List<Integer> storeIds) {
		if(null == storeIds || storeIds.size() <= 0) {
			return null;
		}
		
		List<StockStoreBean> list=stockStoreMapper.getStockStoresByStoreIds(storeIds);
		List<StockStoreDto> stockStoreDtoList = Convert.toStockStoreDto(list);
		if(null == stockStoreDtoList || stockStoreDtoList.size() <= 0) {
			return null;
		}
		
		int initialCapacity = stockStoreDtoList.size();
		Map<Integer, StockStoreDto> map=new HashMap<>(initialCapacity);
		for (StockStoreDto dto : stockStoreDtoList) {
			map.put(dto.getStoreId(), dto);
		}
		
		return map;
	}
	
	/**
	 * 获取仓库ids
	 * @param storeIds
	 * @return
	 */ 
	@Override
	public List<Integer> getStockIds(List<Integer> storeIds) {
		if(null == storeIds || storeIds.isEmpty()) {
			return null;
		}
		return stockStoreMapper.getStockIds(storeIds);
	}
	
	/**
	 * 获取门店仓库信息
	 * 
	 * @param storeId
	 * @return
	 */
	@Override
	public StockStoreDto getStockStoreByStoreId(Integer storeId) {
		
		StockStoreBean stockStore= stockStoreMapper.getByStoreId(storeId);
		StockStoreDto dto = Convert.toStockStoreDto(stockStore);
		
		return dto;
	}

	/**
	 * 保存门店仓库信息
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(StockStoreCreateDto createDto) {
		
		StockStoreBean stockStore=new StockStoreBean();
		BeanUtils.copyProperties(createDto, stockStore);
		
		int num = stockStoreMapper.create(stockStore);
		
		return num==1 ? true : false;
	}

	@Override
	public Map<Integer, Integer> getAllStockStore() {

		List<StockStoreBean> list=stockStoreMapper.getAllStockStore();
        if (CollectionUtils.isEmpty(list)){
        	return Collections.emptyMap();
		}else{
			return	list.stream().collect(Collectors.toMap(StockStoreBean::getStockId,StockStoreBean::getStoreId));
		}

	}

}
